<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('videos', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('description')->nullable();
            $table->string('abbr')->index()->nullable();
            $table->string('picture')->nullable();
            $table->json('videos')->nullable();
            $table->unsignedBigInteger('visits')->index();
            $table->boolean('vip')->unsigned()->index();
            $table->boolean('show')->unsigned()->index();
            $table->unsignedBigInteger('playback')->default(0)->index();
            $table->unsignedBigInteger('like')->default(0)->index();
            $table->unsignedTinyInteger('score')->default(0)->index();
            $table->string('seo_keywords')->nullable();
            $table->string('seo_description', 500)->nullable();
            $table->timestamps();
        });

        DB::statement('ALTER TABLE `videos` ADD FULLTEXT INDEX products_title_fulltext(`title`) WITH PARSER NGRAM');

        Schema::create('actor_video', function (Blueprint $table) {
            $table->foreignIdFor(\App\Models\Actor::class)->constrained()->cascadeOnDelete()->cascadeOnUpdate();
            $table->foreignIdFor(\App\Models\Video::class)->constrained()->cascadeOnDelete()->cascadeOnUpdate();
        });

        Schema::create('category_video', function (Blueprint $table) {
            $table->foreignIdFor(\App\Models\Category::class)->constrained()->cascadeOnDelete()->cascadeOnUpdate();
            $table->foreignIdFor(\App\Models\Video::class)->constrained()->cascadeOnDelete()->cascadeOnUpdate();
        });

        Schema::create('special_video', function (Blueprint $table) {
            $table->foreignIdFor(\App\Models\Special::class)->constrained()->cascadeOnDelete()->cascadeOnUpdate();
            $table->foreignIdFor(\App\Models\Video::class)->constrained()->cascadeOnDelete()->cascadeOnUpdate();
        });

        Schema::create('tag_video', function (Blueprint $table) {
            $table->foreignIdFor(\App\Models\Tag::class)->constrained()->cascadeOnDelete()->cascadeOnUpdate();
            $table->foreignIdFor(\App\Models\Video::class)->constrained()->cascadeOnDelete()->cascadeOnUpdate();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('tag_video');
        Schema::dropIfExists('special_video');
        Schema::dropIfExists('category_video');
        Schema::dropIfExists('actor_video');

        Schema::dropIfExists('videos');
    }
};
